using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Turners.DMS.Features.DMS_Lists
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("6b318dcc-9569-4a61-83da-6496fced25e2")]
    public class DMS_ListsEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        string invoiceSite = "Invoices";
        string gatsSite = "GATs";
        string creditApplication = "CreditApplications";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;

            using (SPSite site = new SPSite(siteCollection.ID))
            {
                using (SPWeb dms = site.RootWeb)
                {
                    //create branches list in site collection
                    //SPListTemplate template = dms.ListTemplates["Branch List Definition"];
                    SPListTemplate invoiceListTemplate = dms.ListTemplates["Invoice List Definition"];
                    //if (dms.Lists.TryGetList("Branches") == null)
                    //{
                    //    Guid listId = dms.Lists.Add("Branches", "Turners Branches", template);
                    //}

                    //create Credit Applicationis document library in Credit Applications subsite
                    SPWeb ca = dms.Webs[creditApplication];
                    SPListTemplate docTemplate = dms.ListTemplates["Turners Document Definition"];
                    if (ca.Lists.TryGetList("CreditApplications") == null)
                    {
                        Guid caDocId = ca.Lists.Add("CreditApplications", "Turners Credit Application", docTemplate);
                    }
                    //create GATs document library in GATs subsite
                    SPWeb gats = dms.Webs[gatsSite];
                    Guid gatsDocId = gats.Lists.Add("GATs", "Turners GATs", docTemplate);

                    //create current year invoices subsite
                    string currentYear = DateTime.Now.Year.ToString();
                    SPWeb invoices = dms.Webs[invoiceSite].Webs[currentYear];
                    if (invoices != null)
                    {
                        if (invoices.Lists.TryGetList("InvoiceDocuments") == null)
                        {
                            Guid invoicedocId = invoices.Lists.Add("InvoiceDocuments", "Invoice Documents", docTemplate);
                        }
                        if (invoices.Lists.TryGetList("Invoices") == null)
                        {
                            Guid invoiceListId = invoices.Lists.Add("Invoices", "Invoices", invoiceListTemplate);
                        }
                    }
                }
            }

            

        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
